﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KJBK;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class EditPM : System.Web.UI.Page
{
    /// <summary>
    /// 科技报刊操作对象
    /// </summary>
    private KJBK_DAO kjbkDAO = new KJBK_DAO();
    /// <summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        String wjm = HttpUtility.UrlDecode(Request["wjm"]);
        if (null == wjm||"".Equals(wjm))
        {
            message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }

        //指定文件名的经济报刊
        KJBK_PM kjbkPM = this.kjbkDAO.GetKJBK_PMByWjm(wjm);

        if (null == kjbkPM)
        {
            message = "错误：没有找到该科技报刊";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }


      

        //获取对应的分类信息
        KJBK_FL kjbkFL = this.kjbkDAO.GetKJBK_FLByFlbm(kjbkPM.flbm);

        if (null == kjbkFL)
        {
            message = "错误：没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //设置返回按钮链接
        String flbm = (wjm.Split('-'))[0];
        this.backHyperLink.NavigateUrl = GlobalKJBK.LISTPM_PAGE + "?catalog=" + flbm;
       
        //分类编码
        this.flbmTextBox.Text = kjbkFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kjbkFL.flmc;
        //索引编码
        this.sybmTextBox.Text=kjbkPM.sybm;
        //年份
        this.nfTextBox.Text = kjbkPM.nf.ToString();
        //期号
        this.qhTextBox.Text = kjbkPM.yf.ToString();

        //标题
        this.btTextBox.Text = kjbkPM.bt;
       
        //报告日期
        this.rqTextBox.Text = kjbkPM.rq.ToString();
        
        //栏目
        this.lmTextBox.Text = kjbkPM.lm;
        //作者
        this.zzTextBox.Text = kjbkPM.zz;
        //文件名
        this.wjmHyperLink.Text = kjbkPM.wjm;
        this.wjmHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE + "?fileName=" + kjbkPM.wjm;
        //原文件名
        this.ywjmTextBox.Text = kjbkPM.ywjm;
        //登录人
        this.dlrTextBox.Text = kjbkPM.dlr;
        //版号
        this.bhTextBox.Text = kjbkPM.bh;
        //更新日期
        this.gxrqTextBox.Text = kjbkPM.gxrq.ToString();
        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的科技报刊篇名信息
    /// </summary>
    /// <returns></returns>
    private KJBK_PM GetKJBKPM()
    {
        KJBK_PM kjbkPM = new KJBK_PM();
        //索引编码
        kjbkPM.sybm = this.sybmTextBox.Text;
        //分类编码
        kjbkPM.flbm = this.flbmTextBox.Text;
        //文件名
        kjbkPM.wjm = this.wjmHyperLink.Text;
        //原文件名
        kjbkPM.ywjm = this.ywjmFileUpload.FileName;
        //标题
        kjbkPM.bt = this.btTextBox.Text.Trim();
        //作者
        kjbkPM.zz = this.zzTextBox.Text.Trim();
        //日期
        kjbkPM.rq = DateTime.Parse(this.rqTextBox.Text);
        //版号
        kjbkPM.bh = this.bhTextBox.Text.Trim();
        //栏目
        kjbkPM.lm = this.lmTextBox.Text.Trim();
        
        //年份
        kjbkPM.nf = Int32.Parse(this.nfTextBox.Text);
        //月份
        kjbkPM.yf = Int32.Parse(this.qhTextBox.Text);
        //更新日期
        kjbkPM.gxrq = DateTime.Parse(this.rqTextBox.Text);
        //登录人
        kjbkPM.dlr = this.dlrTextBox.Text;
        //全文
        kjbkPM.qw = "-1";

        return kjbkPM;

    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateRQ())
        {
            return;
        }
        if (!this.ValidateBT())
        {
            return;
        }
        if (!ValidateYWJMFileUpload())
        {
            return;
        }

        if (null != this.ywjmFileUpload && null != this.ywjmFileUpload.FileName && !"".Equals(this.ywjmFileUpload.FileName))
        {
            if (!OperationFile.Upload(this.ywjmFileUpload, this.wjmHyperLink.Text))
            {
                Response.Write("<Script>alert('修改科技报刊篇名信息失败')</Script>");
                return;
            }
        }
        KJBK_PM kjbkPM = this.GetKJBKPM();


        if (OperationFile.Upload(this.ywjmFileUpload, kjbkPM.wjm))
        {
            if (this.kjbkDAO.UpdateKJBK_PM(kjbkPM,((User)Session["login"]).name))
            {
                Response.Write("<Script>alert('修改科技报刊篇名信息成功')</Script>");
                this.NextPM();
            }
        }
        else
        {
            Response.Write("<Script>alert('修改科技报刊篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        KJBK_PM kjbkPM = this.kjbkDAO.GetKJBK_PMByWjm(this.wjmHyperLink.Text);
        if (null == kjbkPM)
        {
            this.errorLabel.Text = "错误：要修改的文件名不存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.rqTextBox.Text = this.rqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.rqTextBox.Text.Trim());
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBT()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.btTextBox.Text||"".Equals(this.btTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            //this.errorLabel.Text = "错误：请选择上传的文件";
            return flag;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysHiddenField.Value = pdf.NumberOfPages.ToString();
                    this.ywjmTextBox.Text = this.ywjmFileUpload.FileName;
                }

                pdf.Close();
            }
        }
        return flag;
    }

    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalKJBK.EDITPM_PAGE + "?wjm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalKJBK.EDITPM_PAGE + "?wjm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String wjm = this.GetDirectionPM(1);
        if (null == wjm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalKJBK.EDITPM_PAGE + "?wjm=" + wjm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后四位流水号，并在其基础上加1
        int endIndex = this.wjmHyperLink.Text.LastIndexOf(".");

        String phString = this.wjmHyperLink.Text.Substring(endIndex - 4, 4);

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "0" + phString;
        }

        result = this.sybmTextBox.Text + "-" + phString + ".PDF";
        KJBK_PM kjbkPM = this.kjbkDAO.GetKJBK_PMByWjm(result);
        if (null == kjbkPM)
        {
            return null;
        }
        return result;
    }
}